package com.itheima.reggie.controller;

import com.itheima.reggie.comon.ResultInfo;
import com.itheima.reggie.domain.User;
import com.itheima.reggie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    //发送验证码
    @PostMapping("/sendMsg")
    public ResultInfo<Object> setCode(@RequestBody User user) {
        userService.setCode(user);
        return ResultInfo.success(null);
    }

    //登陆
    @PostMapping("/login")
    public ResultInfo login(@RequestBody Map<String, String> map) {
        String phone = map.get("phone");
        String code = map.get("code");
        return userService.login(phone, code);
    }

    //退出
    @PostMapping("/logout")
    public ResultInfo<Object> logout(@RequestHeader("Authorization") String token) {
        //删除token中多余的东西
        token = token.replace("Bearer", "").trim();
        //获取redis里的token
        redisTemplate.delete("TOKEN_" + token);
        return ResultInfo.success(null);
    }
}
